﻿using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Platform;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("pos-payment-type")]
    public class PosPaymentTypeController : BaseController<PosPaymentType, PosPaymentTypePagingDto>
    {
        private readonly IPosPaymentTypeService posPaymentTypeService;

        public PosPaymentTypeController(IPosPaymentTypeService posPaymentTypeService)
        {
            this.posPaymentTypeService = posPaymentTypeService;
        }

        [HttpPost("get-pospayment-type")]
        public async Task<ResponseEntity<PosPaymentType[]>> GetPosPaymentTypes([FromBody] GetPosPaymentTypeDto dto)
        {
            dto.Should().NotBeNull();
            dto.Page ??= 1;
            (dto.Page >= 1).Should().BeTrue();

            var db = await this.Repo.GetDbContextAsync();

            var query = db.Set<PosPaymentType>().IgnoreQueryFilters().AsNoTracking();

            if (dto.UpdateTime != null)
            {
                query = query.Where(x => x.updateTime >= dto.UpdateTime.Value);
            }

            query = query.OrderBy(x => x.updateTime).Page(dto.Page.Value, pageSize: 100);

            var data = await query.ToArrayAsync();

            return new ResponseEntity<PosPaymentType[]>().SetData(data);
        }
    }
}
